package com.etoak;

import com.etoak.service.UserService;
import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.EndpointException;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class CxfApplication {
    public static void main(String[] args) {
        SpringApplication.run(CxfApplication.class);
    }

    @Autowired
    Bus bus;

    @Autowired
    UserService userService;

    @Bean
    public EndpointImpl userServiceEndpoint() throws EndpointException {
        // 给所有服务添加拦截器
//        bus.getInInterceptors().add(new LoggingInInterceptor());
//        QName qName = new QName("http://localhost:9001/user", "UserService");
        EndpointImpl endpoint = new EndpointImpl(bus, userService);
        endpoint.publish("/user");
        return endpoint;
    }

    @Bean
    public JaxWsServerFactoryBean userServiceFactoryBean() {
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setAddress("/user2");
        factory.setServiceClass(UserService.class);
        factory.setServiceBean(userService);
        Server server = factory.create();
        server.start();
        return factory;
    }

//    @Bean
//    public ServletRegistrationBean<CXFServlet> cxfServlet() {
//        ServletRegistrationBean<CXFServlet> registration = new ServletRegistrationBean<>();
//        registration.setServlet(new CXFServlet());
//        registration.addUrlMappings("/ws/*");
//        return registration;
//    }
}
